//
// Created by 大土豆 on 2019-08-21.
// Copyright (c) 2019 Mac. All rights reserved.
//

import Foundation
import UIKit

class GTVideoCoverView : UICollectionViewCell{

    private var coverView : UIImageView?=nil
    private var playButton : UIImageView?=nil
    private var videoUrl : String?=nil
    private var gtVideoToolbar : GTVideoToolbar?=nil

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = UIColor.white
        
        coverView = UIImageView.init(frame: CGRect(x: 0, y: 0, width: frame.size.width, height: frame.size.height - GTVideoToolbarHeight))
        self.addSubview(coverView!)
        
        playButton = UIImageView.init(frame: CGRect(x: (frame.size.width - UI(value:50)) / 2, y: (frame.size.height - GTVideoToolbarHeight - UI(value:50)) / 2, width: UI(value:50), height: UI(value:50)))
        playButton?.image = UIImage.init(named: "videoPlay")
        coverView!.addSubview(playButton!)
        
        gtVideoToolbar = GTVideoToolbar.init(frame: CGRect(x:0,y: coverView!.bounds.size.height,width: frame.size.width,height: GTVideoToolbarHeight))
        self.addSubview(gtVideoToolbar!)
        
        let tapGesture = UITapGestureRecognizer.init(target: self, action: #selector(tapToPlay))
        self.addGestureRecognizer(tapGesture)
    }
    
    public func layoutWithVideoCoverUrl(videoCoverUrl : String,videoUrl : String){
        coverView?.image = UIImage.init(named: videoCoverUrl)
        self.videoUrl = videoUrl;
        gtVideoToolbar?.layoutWithModel(any:"")
    }
    
    @objc private func tapToPlay() {
    //    在当前Item上播放视频
        GTVideoPlayer.shared.playVideoWithUrl(videoUrl: videoUrl!, attchView: coverView!)
    }
}
